/*
 * @(#)NoticeAction.java $version 2013-3-4
 * 
 * Copyright 2013 NADO Group. All rights Reserved.
 * NADO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nado.iims2.community.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nado.extensions.pagination.PaginationInfo;
import com.nado.extensions.util.DataMap;
import com.nado.iims2.community.bo.CommonCodeBO;
import com.nado.iims2.community.bo.NoticeBO;
import com.nado.iims2.community.model.NoticeModel;
import com.nado.iims2.community.util.CommonUtil;

/**
 * The Class NoticeAction.
 * 
 * @author nado
 */
@Controller
public class NoticeAction {
	/** The log. */
	private static Log log = LogFactory.getLog(NoticeAction.class);
	
	private final static String NOTICE_LIST = "/service/noticeList";
	private final static String NOTICE_FORM = "/service/noticeForm";
	
	/** The notice bo. */
	@Autowired
	private NoticeBO noticeBO = null;
	
	/** The common code bo. */
	@Autowired
	private CommonCodeBO commonCodeBO = null;
	
	@RequestMapping(value = "/service/notice", method = RequestMethod.GET)
	public String list(HttpServletRequest request, Model model) {
		// Gets request parameters
		int page = NumberUtils.toInt(request.getParameter("page"), 1);
		String typeCd = StringUtils.defaultIfEmpty(request.getParameter("tcd"), "ALL");
		String startDate = StringUtils.defaultIfEmpty(request.getParameter("sdt"), "");
		String endDate = StringUtils.defaultIfEmpty(request.getParameter("edt"), "");
		List<DataMap> noticeList = new ArrayList<DataMap>();
		PaginationInfo paginationInfo = null;
		
		// Gets notice list & total info
		int count = noticeBO.getNoticeListCount(startDate, endDate, typeCd);
		paginationInfo = new PaginationInfo("default", request, count);
		int offset = CommonUtil.calcOffset(page, paginationInfo.getPageSize());
		noticeList = noticeBO.getNoticeList(startDate, endDate, typeCd, offset, paginationInfo.getPageSize());
		
		// Sets notice info.
		model.addAttribute("noticeCount", count);
		model.addAttribute("noticeList", noticeList);
		model.addAttribute("paginate", paginationInfo);
		model.addAttribute("typeCdList", getNoticeTypeCdList());
		return NOTICE_LIST;
	}
	
	@RequestMapping(value = "/service/notice/form", method = RequestMethod.GET)
	public String form(HttpServletRequest request, Model model) {
		int noticeNo = NumberUtils.toInt(request.getParameter("no"), 0);
		NoticeModel noticeModel = null;
		
		if(noticeNo == 0) {
			noticeModel = new NoticeModel();
		} else {
			noticeModel = noticeBO.getNoticeDetail(noticeNo);
		}
		
		model.addAttribute(noticeModel);
		model.addAttribute("typeCdList", getNoticeTypeCdList());
		return NOTICE_FORM;
	}
	
	@RequestMapping(value = "/service/notice/write", method = RequestMethod.POST)
	public String write(@ModelAttribute NoticeModel model) {
		
		model.setRegistEmpNo("ADMIN");
		
		if(noticeBO.writeNotice(model) == 0) {
			return "redirect:/service/notice/form";
		}
		
		return "redirect:/service/notice";
	}
	
	private List<DataMap> getNoticeTypeCdList() {
		return commonCodeBO.getCommonCodeAndNameList("NOTICE");
	}
}
